﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class EvecVoyageEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string id = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(id))
                {
                    ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                    ReqEvectionVoyage reqEvectionVoyage = ctx.ReqEvectionVoyages.Single(r => r.Id == int.Parse(id));
                    PreLeaveTimeSpan.Value = reqEvectionVoyage.PreLeaveTimeSpan;
                    PreArriveTimeSpan.Value = reqEvectionVoyage.PreArriveTimeSpan;
                    LeavePoint.Value = reqEvectionVoyage.LeavePoint;
                    ArrivePoint.Value = reqEvectionVoyage.ArrivePoint;
                    PrePickTimeSpan.Value = reqEvectionVoyage.PrePickTimeSpan;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            ReqEvectionVoyage reqEvectionVoyage;
            string id = Request.QueryString["id"];
            if (string.IsNullOrEmpty(id))
            {
                reqEvectionVoyage = new ReqEvectionVoyage();
                reqEvectionVoyage.FormNo = Request.QueryString["formNo"];
                ctx.ReqEvectionVoyages.InsertOnSubmit(reqEvectionVoyage);
            }
            else
            {
                reqEvectionVoyage = ctx.ReqEvectionVoyages.Single(r => r.Id == int.Parse(id));
            }
            reqEvectionVoyage.PreLeaveTimeSpan = PreLeaveTimeSpan.Value;
            reqEvectionVoyage.PreArriveTimeSpan = PreArriveTimeSpan.Value;
            reqEvectionVoyage.LeavePoint = LeavePoint.Value;
            reqEvectionVoyage.ArrivePoint = ArrivePoint.Value;
            reqEvectionVoyage.PrePickTimeSpan = PrePickTimeSpan.Value;
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='Succeed';window.close();</script>");
        }
    }
}